package cn.dao.Servlet;

import cn.dao.dao.UserDao;
import cn.dao.entity.UserInfo;
import cn.dao.util.FreemarkerUtil;
import com.google.gson.Gson;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * Created by sunbin on 2017/7/14.
 */
public class UserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           FreemarkerUtil free=new FreemarkerUtil();
        String path= "mybatis-config.xml";
        InputStream is= Resources.getResourceAsStream(path);
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
        SqlSession session=factory.openSession();
        response.setCharacterEncoding("utf-8");
        if("login".equals(request.getParameter("action"))){
            String name=request.getParameter("user");
            String pwd=request.getParameter("pwd");
            UserDao dao = session.getMapper(UserDao.class);
            UserInfo info=dao.isLogin(name,pwd);

            if(info!=null){
                     request.getSession().setAttribute("user",info.getUserName());
                request.getSession().setAttribute("id",info.getId());
                Gson gson=new Gson();
                String success="true";
                String li=gson.toJson(success);



                //JSONArray jso n=JSONArray.fromObject(object)js
                PrintWriter out=response.getWriter();

                out.write(success);
            }else {
                Gson gson=new Gson();
                String success="false";
                String li=gson.toJson(success);



                //JSONArray jso n=JSONArray.fromObject(object)js
                PrintWriter out=response.getWriter();

                out.write(success);

            }


        }else if("success".equals(request.getParameter("action"))){

            Template template = free.getTemplate("welcome.html", getServletContext());
            Map<String ,Object> map=new HashMap<String, Object>();
                       map.put("user",request.getSession().getAttribute("user"));

            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }

        }else if("updates".equals(request.getParameter("action"))){
            Template template = free.getTemplate("password.html", getServletContext());
            Map<String ,Object> map=new HashMap<String, Object>();
            map.put("user",request.getSession().getAttribute("user"));
              map.put("id",request.getSession().getAttribute("id"));
            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }
        }else if("updateone".equals(request.getParameter("action"))){
                int id=Integer.parseInt(request.getParameter("id"));
                String pwd=request.getParameter("oldPassword");
                String pwdold=request.getParameter("newPassword");
            UserDao dao = session.getMapper(UserDao.class);
           int num= dao.updateUser(id,pwdold,pwd);
           session.commit();
           session.close();
           Gson gson=new Gson();
            String s = gson.toJson(num);
            PrintWriter out=response.getWriter();
           out.write(s);

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             doPost(request,response);
    }
}
